package com.xucy.springboot.service;

import com.xucy.springboot.dao.StudentDao;
import com.xucy.springboot.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentDao studentDao;

    //保存一个学生
    @Transactional
    public void saveStudent(Student student) {
        studentDao.save(student);
    }

    //修改一个学生
    public void updateStudent(Student student) {
        studentDao.save(student);
    }


    //根据id删除一个学生
    public void deleteStudent(long id) {
        studentDao.deleteById(id);
    }

    //根据名字查学生
    public List<Student> findByName(String name) {
        return studentDao.findByName(name);
    }

    //根据名字查学生
    public List<Student> findByLastName(String name) {
        return studentDao.findByLastName(name);
    }

    //根据Id查学生
    public Student fingById(long id) {
        return studentDao.findById(id).get();
    }

    //查询所有
    public List<Student> findAll() {
        return studentDao.findAll();
    }

    //查询所有分页
    public List<Student> fingAllPage() {
        return studentDao.findAll(PageRequest.of(1, 2)).getContent();
    }

    //查询排序
    public List<Student> findAllSort() {
        return studentDao.findAll(new Sort(Sort.Direction.ASC, "age"));
    }
}
